package 牛客_面试必刷.Month07.day13;

public class 矩阵的最长路径 {
    int res = 0;
    public int solve (int[][] matrix) {
        // write code here
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                backstacking(matrix,i,j,0,-1);
            }
        }
        return res;
    }

    public void backstacking(int[][] matrix,int i,int j,int count,int prev){
        if(i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length || matrix[i][j] <= prev){
            res = Math.max(count,res);
            return;
        }
        backstacking(matrix,i + 1,j,count + 1,matrix[i][j]);
        backstacking(matrix,i - 1,j,count + 1,matrix[i][j]);
        backstacking(matrix,i,j + 1,count + 1,matrix[i][j]);
        backstacking(matrix,i,j - 1,count + 1,matrix[i][j]);
    }
}
